<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "../../dashboard/mixins/resize";

export default {
  mixins: [resize],
  props: {
    title: {
      type: String,
      default: "",
    },
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({ expectedData, actualData } = {}) {
      this.chart.setOption({
        xAxis: {
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
          boundaryGap: false,
          axisTick: {
            show: false,
          },
        },
        textStyle: {
          color: "#fff",
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 60,
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
          padding: [5, 10],
        },
        yAxis: {
          axisTick: {
            show: false,
          },
          splitNumber: 3,
          splitLine: {
            show: true,
            lineStyle: {
              color: "rgba(255,255,255,0.8)", // 设置分割线颜色为蓝色
              width: 1,
              type: "dashed",
            },
          },
          axisLabel: {
            formatter: "{value} °",
          },
        },
        legend: {
          data: ["俯仰角", "方位角"],
          left: "right",
          textStyle: {
              color:'#fff'
          }
        },
        series: [
          {
            name: "俯仰角",
            itemStyle: {
              normal: {
                color: "#306FFF",
                lineStyle: {
                  color: "#306FFF",
                  width: 2,
                },
              },
            },
            type: "line",
            data: expectedData,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          },{
            name: "方位角",
            itemStyle: {
              normal: {
                color: "#30C9C9",
                lineStyle: {
                  color: "#30C9C9",
                  width: 2,
                },
              },
            },
            type: "line",
            data: actualData,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          }
        ],
      });
    },
  },
};
</script>
